package valleyOfDemon;

import pojo.entity.User;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * POST方法表单请求 字节流 字符流方式获取数据
 */
@WebServlet("/ServletDemo04")
public class ServletDemo04 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //字节流
        ServletInputStream inputStream = req.getInputStream();
        byte[] buff = new byte[1024];
        int len = 0;
        String temp = null;
        while ((len = inputStream.read(buff)) != -1) {
            temp = new String(buff, 0, len);
        }
        System.out.println(URLDecoder.decode(temp, "UTF-8"));


        //字符流
//        BufferedReader reader = req.getReader();
//        String line;
//        String decode = null;
//        while ((line = reader.readLine()) != null) {
//            decode = URLDecoder.decode(line, "UTF-8");
//        }
//        reader.close();
//        System.out.println(decode);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
